//
//  MainViewController.m
//  ArtDog
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MainViewController.h"
#import "HomeNavigationController.h"
#import "HomeMainViewController.h"
#import "CommunityNavigationController.h"
#import "CommunityMainViewController.h"
#import "StoreNavigationController.h"
#import "StoreMainViewController.h"
#import "MineNavigationController.h"
#import "MineMainViewController.h"
#import "UIImage+Common.h"
#import "HLMacro.h"
#import "NSString+Common.h"
#import "MineHomepageViewController.h"
#import "ZWYNetTool.h"
#import "MineHomepageModel.h"
#import "BaseCollectionView.h"
#import "MJRefresh.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[NSUserDefaults standardUserDefaults]objectForKey:kUserDefaultPathKey] == nil) {
        [[NSUserDefaults standardUserDefaults]setObject:@{@"id": @"0", @"token": @"df68e038-143e-41cb-b554-456f78f184fc", @"phoneNumber": @"0", @"password": @"0"} forKey:kUserDefaultPathKey];
    }
    self.viewControllers = [self setupViewControllers];
    [self setupTabBar];
}

- (NSArray<UIViewController *> *)setupViewControllers
{
    NSMutableArray *viewControllers = [NSMutableArray array];
    // 设置首页
    HomeMainViewController *homeMainVC = [[HomeMainViewController alloc]init];
    HomeNavigationController *homeNav = [[HomeNavigationController alloc]initWithRootViewController:homeMainVC];
    [viewControllers addObject:homeNav];
    homeNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[[UIImage imageNamed:@"icon_home"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"icon_home_selected"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    // 设置社区页
    CommunityMainViewController *communityMainVC = [[CommunityMainViewController alloc]init];
    CommunityNavigationController *communityNav = [[CommunityNavigationController alloc]initWithRootViewController:communityMainVC];
    [viewControllers addObject:communityNav];
    communityNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[[UIImage imageNamed:@"icon_community"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[UIImage imageNamed:@"icon_community_selected"]];
    
    // 设置商城页
    StoreMainViewController *storeMainVC = [[StoreMainViewController alloc]init];
    StoreNavigationController *storeNav = [[StoreNavigationController alloc]initWithRootViewController:storeMainVC];
    [viewControllers addObject:storeNav];
    storeNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"商城" image:[[UIImage imageNamed:@"icon_store"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"icon_store_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    // 设置我的页
    MineMainViewController *mineMainVC = [[MineMainViewController alloc]initWithNibName:@"MineMainViewController" bundle:[NSBundle mainBundle]];
    MineHomepageViewController *mineHomePageVC = [[MineHomepageViewController alloc]initWithNibName:@"MineHomepageViewController" bundle:[NSBundle mainBundle]];
    MineNavigationController *mineNav;
    if ([[NSString getUserDefaultId] isEqualToString:@"0"]) {
        mineNav = [[MineNavigationController alloc]initWithRootViewController:mineMainVC];
    } else {
        mineNav = [[MineNavigationController alloc]initWithRootViewController:mineHomePageVC];
        NSDictionary *dic = [NSString getUserDefaultsDic];
        NSString *urlString = [@"http://api.artgoer.cn:8084/artgoer/api/v1/login?" stringByAppendingString:dic[@"phoneNumber"]];
        NSString *paramString = [[[@"mobileNo=" stringByAppendingString:dic[@"phoneNumber"]]stringByAppendingString:@"&password="]stringByAppendingString:dic[@"password"]];
        [ZWYNetTool POST:urlString andBody:paramString andBodyStyle:ZWYBodyString andHeader:@{@"Host": @"api.artgoer.cn:8084"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            if ([result[@"status"] isEqualToString:@"200"]) {
                mineHomePageVC.homePageModel = [MineHomepageModel modelWithDic:result[@"data"]];
                [[NSUserDefaults standardUserDefaults]setObject:@{@"id": [NSString stringWithFormat:@"%@", mineHomePageVC.homePageModel.idField], @"token": mineHomePageVC.homePageModel.token, @"phoneNumber": dic[@"phoneNumber"], @"password": dic[@"password"]} forKey:kUserDefaultPathKey];
                [homeMainVC.collectionView.mj_header beginRefreshing];
            }
        } andFailureBlock:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }
    [viewControllers addObject:mineNav];
    mineNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"icon_mine"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"icon_mine_selected"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return viewControllers;
}

- (void)setupTabBar
{
    self.tabBar.barTintColor = [UIColor clearColor];
    self.tabBar.tintColor = [UIColor blackColor];
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage shadowImageWithWidth:SCREEN_WIDTH];
    
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
